#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, p;
int nums[N];
int ret = 0;

int solve(int numin, int nummax)
{
    int left = 0, right = n - 1, ans1 = -1, ans2 = -1;
    while (left <= right)
    {
        int mid = (right - left) / 2 + left;
        if (nums[mid] >= numin)
        {
            ans1 = mid;
            right = mid - 1;
        }
        else
            left = mid + 1;
    }
    if(ans1==-1) return 0;
    left = 0, right = n - 1;
    while (left <= right)
    {
        int mid = (right - left) / 2 + left;
        if (nums[mid] <= nummax)
        {
            ans2 = mid;
            left = mid + 1;
        }
        else
            right = mid - 1;
    }
    if(ans1==-1||ans2==-1) return 0;
    else return ans2-ans1+1;
}
int main()
{
    cin >> n >> p;
    for (int i = 0; i < n; i++)
        cin >> nums[i];
    sort(nums, nums + n);
    for (int k = 1; k <= 1000000; k++)
    {
        ret = max(ret, solve(k - p, k + p));
    }
    cout<<ret<<endl;
    return 0;
}


//滑动窗口
//思路转换 就是找一个最长的区间满足最大值减最小值<=2*p
//[min-p,min+p] [max-p,max+p] 要是一个区间的 此时一定会有交集 就是max-p<=min+p 就是max-min<=2p
int main()
{
    cin >> n >> p;
    for (int i = 0; i < n; i++)
        cin >> nums[i];
    sort(nums,nums+n);
    for(int left=0,right=0;right<n;right++)
    {
      while(nums[right]-nums[left]>2*p)
      {
          nums[left++];
      }
        ret=max(ret,right-left+1);
    }
    
    cout<<ret<<endl;
    return 0;
}